package edu.ptu.acl.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.google.common.collect.ArrayListMultimap;
import com.google.common.collect.Multimap;
import edu.ptu.acl.dto.RoleModuleTreeDto;
import edu.ptu.acl.entity.Role;
import edu.ptu.acl.entity.RoleModule;
import edu.ptu.acl.exception.ApiException;
import edu.ptu.acl.mapper.RoleMapper;
import edu.ptu.acl.mapper.RoleModuleMapper;
import edu.ptu.acl.service.IRoleAclService;
import edu.ptu.acl.service.IRoleModuleService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import edu.ptu.acl.service.IRoleService;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.AutoPopulatingList;

import javax.annotation.Resource;
import java.util.Collections;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author LinJC
 * @since 2022-03-15
 */
@Service
public class RoleModuleServiceImpl extends ServiceImpl<RoleModuleMapper, RoleModule> implements IRoleModuleService {

    @Resource
    private RoleModuleMapper roleModuleMapper;
    @Resource
    private RoleMapper roleMapper;

    @Override
    public List<RoleModuleTreeDto> getTree() {
        List<RoleModule> allRoleModuleList = roleModuleMapper.selectList(null);

        Multimap<Long, RoleModuleTreeDto> roleModuleTreeDtoMultimap = ArrayListMultimap.create();

        for (RoleModule roleModule : allRoleModuleList) {
            RoleModuleTreeDto roleModuleTreeDto = RoleModuleTreeDto.copyRoleModule2Tree(roleModule);
            roleModuleTreeDtoMultimap.put(roleModule.getParentId(),roleModuleTreeDto);
        }

        List<RoleModuleTreeDto> roleModuleTreeDtoList = (List<RoleModuleTreeDto>)roleModuleTreeDtoMultimap.get(0L);
        Collections.sort(roleModuleTreeDtoList,(d1, d2) -> d1.getSeq()-d2.getSeq());

        recursionRoleModuleTree(roleModuleTreeDtoList,roleModuleTreeDtoMultimap);

        return roleModuleTreeDtoList;
    }

    @Override
    public void validateName(Long parentId, String name) {

        QueryWrapper<RoleModule> roleModuleQueryWrapper = new QueryWrapper<>();
        roleModuleQueryWrapper.eq("parent_id",parentId);
        roleModuleQueryWrapper.eq("name",name);
        if (roleModuleMapper.selectCount(roleModuleQueryWrapper)!=0) {
            throw new ApiException("同级模块下存在同名模块");
        }
    }

    @Override
    public void validateRole(Long parentId) {
        QueryWrapper<Role> roleQueryWrapper = new QueryWrapper<>();
        roleQueryWrapper.eq("parent_id",parentId);
        if (roleMapper.selectCount(roleQueryWrapper)!=0){
            throw new ApiException("模块下面有角色");
        }
        QueryWrapper<RoleModule> roleModuleQueryWrapper = new QueryWrapper<>();
        roleModuleQueryWrapper.eq("parent_id",parentId);
        if (roleModuleMapper.selectCount(roleModuleQueryWrapper)!=0){
            throw new ApiException("请先删除子模块");
        }
    }

    private void recursionRoleModuleTree(List<RoleModuleTreeDto> roleModuleTreeDtoList, Multimap<Long, RoleModuleTreeDto> roleModuleTreeDtoMultimap) {
        if (CollectionUtils.isEmpty(roleModuleTreeDtoList)){
            return;
        }
        for (RoleModuleTreeDto roleModuleTreeDto : roleModuleTreeDtoList) {
            List<RoleModuleTreeDto> sonRoleModuleTreeDtoList = (List<RoleModuleTreeDto>)roleModuleTreeDtoMultimap.get(roleModuleTreeDto.getId());
            Collections.sort(sonRoleModuleTreeDtoList,(d1, d2) -> d1.getSeq()-d2.getSeq());
            roleModuleTreeDto.setSonRoleModuleList(sonRoleModuleTreeDtoList);
            recursionRoleModuleTree(sonRoleModuleTreeDtoList,roleModuleTreeDtoMultimap);
        }
    }


}
